﻿
Framework  sedm.blitz3dogre
Import brl.polledinput
Import maxgui.drivers
'Import brl.d3d9max2d
Import brl.eventqueue
Import brl.timer

'phys:TBullet = New TBullet
'phys.setGravity()

AppTitle = "BlitzOgre3D / Blitz3D-like API"

desk:TGadget = Desktop()
win:TGadget = CreateWindow(AppTitle, (desk.width/2) - 400, (desk.Height/2) - 300, 800, 600, Null, WINDOW_TITLEBAR|WINDOW_STATUS)

ww = win.ClientWidth()
hh = win.ClientHeight()

pnl_opt:TGadget = CreatePanel(4, 4, 200, hh - 8, win, PANEL_GROUP, "Tools")
pnl_cvs:TGadget = CreatePanel(208, 4, ww - 212, hh - 8, win, PANEL_GROUP, "3D View")

cw = pnl_cvs.ClientWidth() - 8
ch = pnl_cvs.ClientHeight() - 8

cvs_b3d:TGadget = CreateCanvas(4, 4, cw, ch, pnl_cvs)
hwnd = QueryGadget(cvs_b3d, QUERY_HWND_CLIENT)

lbl_blurWeight:TGadget = CreateLabel("Blur Weight", 4, 4, pnl_opt.ClientWidth() - 8, 20, pnl_opt)
sld_blurWeight:TGadget = CreateSlider(4, 28, pnl_opt.ClientWidth() - 8, 20, pnl_opt, SLIDER_HORIZONTAL)
SetSliderRange(sld_blurWeight, 0, 61)

Graphics3DExt(cw, ch, 0, 60, hwnd)

AmbientLight(0.1, 0.1, 0.1)

SetShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE)
SetShadowTextureSize(1024)
setShadowFarDistance(100.0)

LoadSkyBox("media.zip/SkyBox.material")

cam = createCamera()

ScreenFX(cam, "Bloom")

player	= CreatePivot()
CameraRange(cam, 0.1, 10000)

EntityParent(cam, player)
PositionEntity(player, 0.0, 10.0, -40.0)

l = CreateLight()
PositionEntity(l, 10.0, 100.0, 100.0)

box 	= LoadMesh("media.zip/box.mesh")
ground	= LoadMesh("media.zip/ground.mesh")

mat = GetBrushByName("Ogre/Compositor/BloomBlend2")
tech = GetBrushTechnique(mat)
pss = GetBrushPass(tech)
pars = GetPassPars(pss)

PositionEntity(ground, 0.0, -5.0, 0.0)

box2	= CopyEntity(box)
PositionEntity(box2, -10.0, 0.0, 7.0)

EntityShadows(ground, False)

timer:TTimer = CreateTimer(60)

Local keycodes:Byte[256]
For i = 0 To 255
	keycodes[i] = False
Next

Local buttons:Byte[6]
For i = 0 To 5
	buttons[i] = False
Next

Local mx:Int
Local my:Int

Type TMouse
	Field lastX:Int
	Field lastY:Int
	
	Method MouseXSpeed:Int(x:Int)
		Return x - lastX
	End Method
	
	Method MouseYSpeed:Int(y:Int)
		Return y - lastY
	End Method
	
	Method Update(x:Int, y:Int)
		lastx = x
		lasty = y
	End Method
End Type
Local m:TMouse = New TMouse

' Shaderparameters
Local blurWeight:Float = 0.0
Local OriginalImageWeight:Float = 1.0
'SetBrushConstantF(pars, "BlurWeight", blurWeight)
'SetBrushConstantF(pars, "OriginalImageWeight", OriginalImageWeight)
					
SetSliderValue(sld_blurWeight, blurWeight)

Repeat
	WaitEvent()
	
	Select EventID()
		Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE	; Exit
		
		Case EVENT_TIMERTICK						
			If keycodes[KEY_W] Then MoveEntity(player, 0.0, 0.0, 0.5)
			If keycodes[KEY_A] Then MoveEntity(player, -0.5, 0.0, 0.0)
			If keycodes[KEY_S] Then MoveEntity(player, 0.0, 0.0, -0.5)
			If keycodes[KEY_D] Then MoveEntity(player, 0.5, 0.0, 0.0)
			
			If buttons[2]
				TurnEntity(player, 0.0, -m.MouseXSpeed(mx) * 0.01, 0.0)
				TurnEntity(cam, -m.MouseYSpeed(my) * 0.01, 0.0, 0.0)
			EndIf
						
			RenderWorld()
			m.Update(mx, my)
			
			SetBrushConstantF(pars, "BlurWeight", blurWeight)
		Case EVENT_KEYDOWN
			keycodes[EventData()] = True
			
		Case EVENT_KEYUP
			keycodes[EventData()] = False
			
		Case EVENT_MOUSEMOVE
			mx = EventX()
			my = EventY()
						
		Case EVENT_MOUSEDOWN
			buttons[EventData()] = True
						
		Case EVENT_MOUSEUP
			buttons[EventData()] = False
			
		Case EVENT_GADGETACTION
			Select EventSource()
				Case sld_blurWeight
					blurWeight = Float(SliderValue(sld_blurWeight)) * 0.1
					SetStatusText(win, blurWeight)
			End Select
	End Select
Until KeyDown(KEY_ESCAPE)

phys = Null

End
